/*----------------------------------------------------------------------
  - File name      : STC8Ax_PWM.c
  - Author         : Quark Team
  - Update date    : 2020-08-30                  
  -	Copyright      : Gevico Electronic studio   
  - Module comments: PWM module.
-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
|                               INCLUDES                                |
-----------------------------------------------------------------------*/
#include "STC8Ax_PWM.h"
/*-----------------------------------------------------------------------
|                                 DATA                                  |
-----------------------------------------------------------------------*/
/* None */
/*-----------------------------------------------------------------------
|                               FUNCTION                                |
-----------------------------------------------------------------------*/

/*--------------------------------------------------------
| @Description: PWM counter init function                |
| @param      : PWMCLKSrc_Type CLKSrc,                   |
|               uint16 Value,FUNSTATE State              |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE PWM_CNT_Init(PWMCLKSrc_Type CLKSrc,uint16 Value,FUNSTATE State)
{
  if(Value <= 0x7FFF)
  {
    EAXFR_ENABLE();
    PWMCKS = CLKSrc;
    PWMC = Value;
    PWMCR = (PWMCR & 0x7F) | (State << 7);
    EAXFR_DISABLE();
    return FSC_SUCCESS;
  }
  else return FSC_FAIL;
}

/*--------------------------------------------------------
| @Description: PWM0 road init function                  |
| @param      : PWMStartLevel_Type Level,                |
|               uint16 FValue,uint16 SValue,             |
|               FUNSTATE State                           |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE PWM0_Init(PWMStartLevel_Type Level,uint16 FValue,uint16 SValue,FUNSTATE State)
{
  if(FValue <= 0x7FFF && SValue <= 0x7FFF)
  {
    EAXFR_ENABLE();
    PWMxT1(PWM0T1_ADDRESS) = FValue;
    PWMxT2(PWM0T2_ADDRESS) = SValue;
    PWMxCR(PWM0CR_ADDRESS) = (PWMxCR(PWM0CR_ADDRESS) & 0xBF) | (Level << 6);
    PWMxCR(PWM0CR_ADDRESS) = (PWMxCR(PWM0CR_ADDRESS) & 0x7F) | (State << 7);
    EAXFR_DISABLE();
    return FSC_SUCCESS;
  }
  else return FSC_FAIL;
}

/*--------------------------------------------------------
| @Description: PWM1 road init function                  |
| @param      : PWMStartLevel_Type Level,                |
|               uint16 FValue,uint16 SValue,             |
|               FUNSTATE State                           |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE PWM1_Init(PWMStartLevel_Type Level,uint16 FValue,uint16 SValue,FUNSTATE State)
{
  if(FValue <= 0x7FFF && SValue <= 0x7FFF)
  {
    EAXFR_ENABLE();
    PWMxT1(PWM1T1_ADDRESS) = FValue;
    PWMxT2(PWM1T2_ADDRESS) = SValue;
    PWMxCR(PWM1CR_ADDRESS) = (PWMxCR(PWM1CR_ADDRESS) & 0xBF) | (Level << 6);
    PWMxCR(PWM1CR_ADDRESS) = (PWMxCR(PWM1CR_ADDRESS) & 0x7F) | (State << 7);
    EAXFR_DISABLE();
    return FSC_SUCCESS;
  }
  else return FSC_FAIL;
}

/*--------------------------------------------------------
| @Description: PWM2 road init function                  |
| @param      : PWMStartLevel_Type Level,                |
|               uint16 FValue,uint16 SValue,             |
|               FUNSTATE State                           |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE PWM2_Init(PWMStartLevel_Type Level,uint16 FValue,uint16 SValue,FUNSTATE State)
{
  if(FValue <= 0x7FFF && SValue <= 0x7FFF)
  {
    EAXFR_ENABLE();
    PWMxT1(PWM2T1_ADDRESS) = FValue;
    PWMxT2(PWM2T2_ADDRESS) = SValue;
    PWMxCR(PWM2CR_ADDRESS) = (PWMxCR(PWM2CR_ADDRESS) & 0xBF) | (Level << 6);
    PWMxCR(PWM2CR_ADDRESS) = (PWMxCR(PWM2CR_ADDRESS) & 0x7F) | (State << 7);
    EAXFR_DISABLE();
    return FSC_SUCCESS;
  }
  else return FSC_FAIL;
}

/*--------------------------------------------------------
| @Description: PWM3 road init function                  |
| @param      : PWMStartLevel_Type Level,                |
|               uint16 FValue,uint16 SValue,             |
|               FUNSTATE State                           |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE PWM3_Init(PWMStartLevel_Type Level,uint16 FValue,uint16 SValue,FUNSTATE State)
{
  if(FValue <= 0x7FFF && SValue <= 0x7FFF)
  {
    EAXFR_ENABLE();
    PWMxT1(PWM3T1_ADDRESS) = FValue;
    PWMxT2(PWM3T2_ADDRESS) = SValue;
    PWMxCR(PWM3CR_ADDRESS) = (PWMxCR(PWM3CR_ADDRESS) & 0xBF) | (Level << 6);
    PWMxCR(PWM3CR_ADDRESS) = (PWMxCR(PWM3CR_ADDRESS) & 0x7F) | (State << 7);
    EAXFR_DISABLE();
    return FSC_SUCCESS;
  }
  else return FSC_FAIL;
}

/*--------------------------------------------------------
| @Description: PWM4 road init function                  |
| @param      : PWMStartLevel_Type Level,                |
|               uint16 FValue,uint16 SValue,             |
|               FUNSTATE State                           |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE PWM4_Init(PWMStartLevel_Type Level,uint16 FValue,uint16 SValue,FUNSTATE State)
{
  if(FValue <= 0x7FFF && SValue <= 0x7FFF)
  {
    EAXFR_ENABLE();
    PWMxT1(PWM4T1_ADDRESS) = FValue;
    PWMxT2(PWM4T2_ADDRESS) = SValue;
    PWMxCR(PWM4CR_ADDRESS) = (PWMxCR(PWM4CR_ADDRESS) & 0xBF) | (Level << 6);
    PWMxCR(PWM4CR_ADDRESS) = (PWMxCR(PWM4CR_ADDRESS) & 0x7F) | (State << 7);
    EAXFR_DISABLE();
    return FSC_SUCCESS;
  }
  else return FSC_FAIL;
}

/*--------------------------------------------------------
| @Description: PWM5 road init function                  |
| @param      : PWMStartLevel_Type Level,                |
|               uint16 FValue,uint16 SValue,             |
|               FUNSTATE State                           |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE PWM5_Init(PWMStartLevel_Type Level,uint16 FValue,uint16 SValue,FUNSTATE State)
{
  if(FValue <= 0x7FFF && SValue <= 0x7FFF)
  {
    EAXFR_ENABLE();
    PWMxT1(PWM5T1_ADDRESS) = FValue;
    PWMxT2(PWM5T2_ADDRESS) = SValue;
    PWMxCR(PWM5CR_ADDRESS) = (PWMxCR(PWM5CR_ADDRESS) & 0xBF) | (Level << 6);
    PWMxCR(PWM5CR_ADDRESS) = (PWMxCR(PWM5CR_ADDRESS) & 0x7F) | (State << 7);
    EAXFR_DISABLE();
    return FSC_SUCCESS;
  }
  else return FSC_FAIL;
}

/*--------------------------------------------------------
| @Description: PWM6 road init function                  |
| @param      : PWMStartLevel_Type Level,                |
|               uint16 FValue,uint16 SValue,             |
|               FUNSTATE State                           |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE PWM6_Init(PWMStartLevel_Type Level,uint16 FValue,uint16 SValue,FUNSTATE State)
{
  if(FValue <= 0x7FFF && SValue <= 0x7FFF)
  {
    EAXFR_ENABLE();
    PWMxT1(PWM6T1_ADDRESS) = FValue;
    PWMxT2(PWM6T2_ADDRESS) = SValue;
    PWMxCR(PWM6CR_ADDRESS) = (PWMxCR(PWM6CR_ADDRESS) & 0xBF) | (Level << 6);
    PWMxCR(PWM6CR_ADDRESS) = (PWMxCR(PWM6CR_ADDRESS) & 0x7F) | (State << 7);
    EAXFR_DISABLE();
    return FSC_SUCCESS;
  }
  else return FSC_FAIL;
}

/*--------------------------------------------------------
| @Description: PWM7 road init function                  |
| @param      : PWMStartLevel_Type Level,                |
|               uint16 FValue,uint16 SValue,             |
|               FUNSTATE State                           |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE PWM7_Init(PWMStartLevel_Type Level,uint16 FValue,uint16 SValue,FUNSTATE State)
{
  if(FValue <= 0x7FFF && SValue <= 0x7FFF)
  {
    EAXFR_ENABLE();
    PWMxT1(PWM7T1_ADDRESS) = FValue;
    PWMxT2(PWM7T2_ADDRESS) = SValue;
    PWMxCR(PWM7CR_ADDRESS) = (PWMxCR(PWM7CR_ADDRESS) & 0xBF) | (Level << 6);
    PWMxCR(PWM7CR_ADDRESS) = (PWMxCR(PWM7CR_ADDRESS) & 0x7F) | (State << 7);
    EAXFR_DISABLE();
    return FSC_SUCCESS;
  }
  else return FSC_FAIL;
}

/*--------------------------------------------------------
| @Description: PWM abnormal detection init function     |
| @param      : PWMADSMode_Type Mode,                    |
|               PWMABSignal_Type Signal,                 |
|               FUNSTATE State                           |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE PWM_ABD_Init(PWMADSMode_Type Mode,PWMABSignal_Type Signal,FUNSTATE State)
{
  EAXFR_ENABLE();
  PWMFDCR = (PWMFDCR & 0xF9) | (Mode);
  PWMFDCR = (PWMFDCR & 0x3F) | (Signal);
  PWMFDCR = (PWMFDCR & 0xF7) | (State);
  EAXFR_DISABLE();
  return FSC_SUCCESS;
}

/*--------------------------------------------------------
| @Description: PWM trigger associated ADC init function |
| @param      : uint16 Value,FUNSTATE State              |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE PWM_ETADC_Init(uint16 Value,FUNSTATE State)
{
  if(Value <= 0x7FFF)
  {
    EAXFR_ENABLE();
    TADCP = Value;
    PWMCFG = (PWMCFG & 0xBF) | (State << 6);
    EAXFR_DISABLE();  
    return FSC_SUCCESS;
  }
  else return FSC_FAIL;  
}




/*-----------------------------------------------------------------------
|          END OF FLIE        (C) COPYRIGHT Gevico Electronics          | 
-----------------------------------------------------------------------*/



